<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img :src="pic" class="img" alt="头像" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginformref"
        :model="loginform"
        label-width="0px"
        class="login_form"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="loginform.username" prefix-icon="el-icon-s-check"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginform.password"
            prefix-icon="el-icon-goods"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="buts">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetloginform">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { async } from "q";
import Cookies from "js-cookie";

export default {
  name: "login",
  data() {
    return {
      pic: "/static/image/admin.jpg",
      // 绑定表单数据对象
      loginform: {
        username: "admin",
        password: "123456"
      },
      //表单检验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //重置表单
    resetloginform() {
      this.$refs.loginformref.resetFields();
    },
    //表单预验证
    login() {
      this.$refs.loginformref.validate(async val => {
        if (!val) return;
        this.$api
          .login()
          .then(function(res) {
            alert(res.token);
            Cookies.set("token", res.token); // 放置token到Cookie
            this.$router.push("/") // 登录成功，跳转到主页
          })
          .catch(function(res) {
            alert(res);
          });
      });
    }
  }
};
</script>
<style  scoped>
.login_container {
  height: 100%;
  background-color: rgb(20, 19, 19);
}
.login_box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 4px;
  position: absolute; /*绝对定位*/
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); /*移动box*/
}
.avatar_box {
  height: 130px;
  width: 120px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.buts {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>